package com.buyouchina.web.action.product;

import java.io.PrintWriter;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.buyouchina.bean.product.Category;
import com.buyouchina.bean.product.Outlets;
import com.buyouchina.bean.product.Product;
import com.buyouchina.bean.product.Record;
import com.buyouchina.service.product.CategoryService;
import com.buyouchina.service.product.ProductService;
import com.buyouchina.utils.Pagination;
import com.buyouchina.utils.SiteUrl;
import com.buyouchina.utils.ToJson;
import com.buyouchina.web.action.BaseAction;
import com.buyouchina.web.action.privilege.Permission;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Scope("prototype")
@Controller("ProductAction")
public class ProductAction extends BaseAction implements ModelDriven<Product>,
		Preparable {

	private static final long serialVersionUID = 1L;

	@Resource(name = "productServiceBean")
	private ProductService productService;

	@Resource(name = "categoryServiceBean")
	private CategoryService categoryService;

	private List<Category> categoryList = new ArrayList<Category>();
	private List<Record> recordList = new ArrayList<Record>();
	private List<Product> productList = new ArrayList<Product>();

	private Product product;
	private Integer productid;
	private Integer categoryid;
	private String operate;

	// 查询仓库中得货物(不包括已经下架或者删除的货物) 货物列表
	public String store() {
		Outlets outlets = (Outlets) session.get("outlets");
		if (outlets != null) {
			int recordCount = productService.getRecordCount(product
					.getForSale(), outlets.getId());
			Pagination pg = new Pagination(request, response);
			pg.setRecordCount(recordCount);
			productList = productService.getStore(product.getForSale(), outlets
					.getId(), pg.getFirstResult(), pg.getPageSize());
			request.setAttribute("pagination", pg);
		}
		categoryList = categoryService.getScrollData().getResultlist();
		return SUCCESS;
	}

	// 新建产品
	@Permission(model = "product", privilegeValue = "insert")
	public String add() {

		product.setCategory(categoryService.find(categoryid));
		product.setDate(new Date());
		product.setOutlets((Outlets) session.get("outlets"));
		productService.save(product);

		request.setAttribute("message", "新建产品成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("store.list"));
		return MESSAGE;
	}

	@Permission(model = "product", privilegeValue = "update")
	public String edit() {
		categoryList = categoryService.getScrollData().getResultlist();
		return "edit";
	}

	// 上/下架商品
	public String del() {
		Boolean forSale = product.getForSale();
		product = productService.find(product.getId());
		product.setForSale(forSale);
		productService.update(product);
		if (forSale == true) {
			request.setAttribute("message", "产品上架成功");
			request.setAttribute("urladdress", SiteUrl.readUrl("unstore.list"));
		} else {
			request.setAttribute("message", "产品下架成功");
			request.setAttribute("urladdress", SiteUrl.readUrl("store.list"));
		}

		return MESSAGE;
	}

	// 进/出货
	public String getProduct() throws Exception {

		if (operate == "save") { // 如果保存
			return SUCCESS;
		} else { // 非保存操作,只跳转
			return "purchase";
		}
	}

	// 更新
	@Permission(model = "product", privilegeValue = "update")
	public String update() {
		product.setCategory(categoryService.find(categoryid));
		productService.update(product);
		request.setAttribute("message", "产品修改成功!!");
		request.setAttribute("urladdress", SiteUrl.readUrl("store.list"));
		return "message";
	}

	// 查询单件商品
	public void getProductInfo() {
		product = productService.find(productid);
		ToJson.OutJSONObject(response, product);
	}

	// 验证货物编号code是否有重复
	public void checkCode() {
		Boolean flag = false;
		String code = product.getCode();
		if (productService.findByCode(code)) {
			flag = true;
		}
		try {
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Product getModel() {
		return product;
	}

	public void prepare() throws Exception {
		if (productid == null || "".equals(productid)) {
			product = new Product();
		} else {
			product = productService.find(productid);
		}
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public List<Record> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<Record> recordList) {
		this.recordList = recordList;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public Integer getProductid() {
		return productid;
	}

	public void setProductid(Integer productid) {
		this.productid = productid;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Integer getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(Integer categoryid) {
		this.categoryid = categoryid;
	}

}
